App({
    onLaunch: function() {
        // 初始化 uniCloud
        uniCloud.init({
        provider: 'aliyun',
        spaceId: 'mp-5c931254-afb8-4575-96ed-ba0905c9786c',
        clientSecret: 'sjCww5Z/+H8gSWZ9bcHZkw==',
        debug: false,
        useRuntime: true
        })
        this.handleLogin();
    },
    async handleLogin() {
        const user = uni.getStorageSync('userInfo');
        //1.本地缓存存在openId->action: 'getInfo' 判断用户是否存在
        //1.1用户存在则进入个人中心
        //1.2用户不存在进入用户名填写
        //2.本地没有缓存的openId ->掉用login得到code->得到openid
        //2.1openid要先去用户表里面查 存在则直接进入个人中心
        //2.2openid要先去用户表里面查 不存在则直接用户昵称信息保存
		if (user && user.openId) {
			const obj = await this.handleExistUser(user);
			if (obj.result.code == 0 && obj.result.data) {
				uni.setStorageSync('userInfo', obj.result.data);
				uni.switchTab({
					url: '/pages/profile/profile',
				});
			}
		} else {
			this.toLogin();
		}
	},
	async handleExistUser(user) {
	    const userInfo = await uniCloud.callFunction({
	        name: 'user',
	        data: {
	            action: 'getInfo',
	            data: user,
	        },
	    });
	    return userInfo;
	},
	async toLogin() {
	    try {
	        const result = await uni.login({
	            provider: 'weixin', //使用微信登录
	        });
	        // 获取openid
	        const loginResult = await uni.login();
	        if (loginResult.code) {
	            try {
	                // 调用云函数获取openid
	                const wxResult = await uniCloud.callFunction({
	                    name: 'login',
	                    data: {
	                        code: loginResult.code,
	                    },
	                });
	                if (wxResult.result.code == 0) {
						debugger
	                    // 跳转到验证登录
	                    if (wxResult.result.openid) {
	                        //校验用户信息是否存在
	                        const obj = await this.handleExistUser({ openId: wxResult.result.openid });
	                        if (obj.result.code == 0 && obj.result.data) {
	                            let { openId, avatarUrl, nickName } = obj.result.data;
	                            uni.setStorageSync('userInfo', { openId, avatarUrl, nickName });
	                            uni.switchTab({
	                                url: '/pages/profile/profile',
	                            });
	                        } else {
	                            uni.redirectTo({
	                                url: `/pages/auth/auth?openId=${wxResult.result.openid}`,
	                            });
	                        }
	                    }
	                } else {
	                    console.warn('获取openId失败，使用昵称代替');
	                }
	            } catch (e) {
	                console.error('调用login云函数失败:', e);
	            }
	        }
	    } catch (e) {
	        console.error('处理用户登录失败:', e);
	        uni.showToast({
	            title: e.message || '登录失败，请重试',
	            icon: 'none',
	        });
	    }
	    complete: () => {
	        this.loading = false;
	    };
	},
}); 